﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections.Specialized;
using System.Linq;
using System.IO;

namespace LuckStar
{
    public partial class MainForm : Form
    {
        Lucky luck;
        public MainForm()
        {
            InitializeComponent();
          
        }
        //解决闪烁的问题
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;
                return cp;
            }
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            this.panel1.Parent = this.pictureBox1;
            SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
            this.UpdateStyles();
            ShowLuckInfo(true);
        }

        /// <summary>
        /// 删除奖项
        /// </summary>
        /// <param name="PrizeID"></param>
        /// <returns></returns>
        public bool DelPrize(string PrizeID)
        {
            LuckAction.DelPrize(PrizeID);
            ShowLuckInfo(true);
            return true;
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="pri"></param>
        /// <returns></returns>
        public bool SavePrize(Prize pri)
        {
            LuckAction.SavePrize(pri);
            ShowLuckInfo(true);
            return true;
        }

        /// <summary>
        /// 展示信息
        /// </summary>
        public void ShowLuckInfo(bool isReload)
        {
            if(isReload||luck==null)
                luck = LuckAction.LoadData();             

            this.Text = luck.Title;
            if (File.Exists(luck.BackgroudImage))
            {
                pictureBox1.Load(luck.BackgroudImage);           
            }
            panel1.Controls.Clear();
            int height = luck.PrizeList.Count*160;
            if (height < 400)
                height = 400;
            int width = luck.PrizeList.Count * 330;
            if (width > this.Width)
                width = this.Width;
            panel1.Width = width;
            panel1.Height = height;
            panel1.Location = new Point((this.Width-width)/2, this.Height - height);

            int totalCount = 0;//总的中间名额
            int realCount = 0;// 剩余的中奖名额
            int realPcount = 0;//剩余的奖项数量
            int hasPcount = 0;//已经中奖的人数
            foreach (Prize p in luck.PrizeList)
            {
                totalCount += p.Count;
                if (p.WinnersList.Count < p.Count)
                {
                    realCount += p.Count;
                    realPcount++;
                }
                else
                {
                    hasPcount += p.WinnersList.Count;
                }
                panel1.Controls.Add(new PrizeControl(luck, p));
            }
            if (luck.UserList.Count < 1)
                tool_info.Text = "请导入抽奖用户数据";
            else
            {
                tool_info.Text = "总共" + luck.UserList.Count + "个抽奖用户，" + luck.PrizeList.Count + "个奖项" + totalCount
                    + "个名额，综合中奖率" + (totalCount * 1.0 / luck.UserList.Count * 100).ToString("f2") + "%，当前剩余" + (luck.UserList.Count - hasPcount)
                    + "个未中奖用户，" + realPcount + "个奖项" + realCount + "个名额，实时中奖率" + (realCount * 1.0 / (luck.UserList.Count - hasPcount) * 100).ToString("f2") + "%    ";
            }
        }
       

        private void MainForm_SizeChanged(object sender, EventArgs e)
        {
            ShowLuckInfo(false);
        }

        private void clearW_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确定清除中奖纪录么？", "清除中奖纪录", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                luck = LuckAction.ClearData(luck);               
                ShowLuckInfo(false);
                MessageBox.Show("清除成功！");
            }
          
            
        }

        private void savedata_Click(object sender, EventArgs e)
        {
            LuckAction.SaveData(luck);
            MessageBox.Show("保存成功！");

        }

        private void loaduser_Click(object sender, EventArgs e)
        {
            luck=LuckAction.LoadUserData(luck);
            MessageBox.Show("加载成功！");
        }

        private void loaddata_Click(object sender, EventArgs e)
        {
            luck = LuckAction.LoadData();
            MessageBox.Show("加载成功！");
        }

        private void 添加奖项ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new PrizeEditForm(this,null).ShowDialog();
        }

        private void 操作说明ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new AboutBox().ShowDialog();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tool_help_Click(object sender, EventArgs e)
        {
            new AboutBox().ShowDialog();
        }

        private void 设置背景图片ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            of.Filter = "图片|*.jpg;*.png;*.gif;*.jpeg;*.bmp";
            of.Multiselect = false;
            if (of.ShowDialog() == DialogResult.OK)
            {
                luck.BackgroudImage = of.FileName;
                LuckAction.SaveData(luck);
                ShowLuckInfo(true);
            }

           
        }

    }
}
